<?php
/**
 * Created by PhpStorm.
 * User: liuhong
 * Date: 2019-06-29
 * Time: 13:34
 */

namespace app\admin\controller;
use think\Db;
use app\common\model\Alipay;
use app\common\model\Mall;
use app\common\model\OrderRecharge;

class Recharge extends Base
{

    //查询报单列表
    public function lists(){
        $input=input();
        $page=$input['page'];//min($input['page'],1);
        if($page<=0){
            $page=1;
        }
        $pagenum=20;
        $where['checked'] = 1;
        if($this->manager['mall_id']==0 && !empty($input['mall_id'])){
            $where['mall_id']=$input['mall_id'];
        }elseif($this->manager['mall_id']!=0){
            $where['mall_id']=$this->manager['mall_id'];
        }
        if(!empty($input['order_sn'])){
            $where['order_sn']=$input['order_sn'];
        }
        if(!empty($input['start_create_time'])){
            $where['create_time']=array('EGT',$input['start_create_time']);
        }
        if(!empty($input['start_create_time']) && !empty($input['end_create_time'])){
            $where['create_time']=array(array('EGT',$input['start_create_time']),array('ELT',$input['end_create_time']),"AND");
        }
        $count=Db::name('order_recharge')->where($where)->count();
        $list=OrderRecharge::getPage($where,$page,$pagenum);
        if($list){
            foreach($list as $k=>$v) {
                $mallInfo = Mall::getOneById($v['mall_id'],'id,name');
                $list[$k]['mall_name'] = $mallInfo['name'];
            }
        }
        $backData['code']=200;
        $backData['msg']='查询成功';
        $backData['total']=ceil($count/$pagenum);
        $backData['size']=$pagenum;//ceil($count/$pagenum);
        $backData['page']=$page;
        $backData['data']=$list;
        echo json_encode($backData);exit();
    }


    //充值信息
    public function charge(){
        $input=input();
        if($input['money']<=0){
            $this->ajax(202,'充值金额不能小于0');
        }
        $add['money']=$input['money'];
        $add['mall_id']=$this->mall_id;
        $add['mall_name']=$this->mall['name'];
        $add['create_time']=date('Y-m-d H:i:s');
        $add['remark']=$input['remark'];
        $add['order_sn']=date('YmdHis').rand(1000,9999);

        $order_id=Db::name('order_recharge')->insertGetId($add);
        if($order_id){
            $alipay = new Alipay();
            $data['notify_url'] = getDomain().'/notify/alipaycallback/mall_id/1';//异步通知
            $call_url = getDomain().'/w/#/pages/index/chargelist';//同步通知
            $data['return_url'] = $call_url;
            $data['show_url'] = $call_url;
            $data['out_trade_no'] = $add['order_sn'];//$order['order_sn'];//商城订单号
            $data['subject'] = '商品积分充值';//$order['order_title'];//商品名称
            $data['total_fee'] =$input['money'];//交易金额
            $url=$alipay->wappay($data);
            $this->ajax(200,'success',$url);
        }else{
            $this->ajax(202,'返回错误');
        }
    }

    public function refundCharge(){
        $input=input();
        if($this->mall_id!=0){
            $this->ajax(202,'您没有操作权限');
        }
        if(!isset($input['id'])){
            $this->ajax(202,'请输入退款订单ID');
        }

    }

}